<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

class RestoranModelformrestoran extends JModel
{

	var $_id = null;
	var $_data = null;

	function __construct()
	{
		parent::__construct();

		$array = JRequest::getVar('cid', array(0), '', 'array');
		$this->setId((int)$array[0]);
	}

	function setId($id)
	{
		// Set weblink id and wipe data
		$this->_id		= $id;
		$this->_data	= null;
	}


	function getData()
	{   $mbuh = JRequest::getVar('task');
        
		// Load the data
		if (empty( $this->_data )&&$mbuh=='edit') {
            $id = $this->_id;
            $url = $this->getUrl();
            $url_server = $url."food"."/".$id;
            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL,$url_server);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            $data = curl_exec($ch);

            curl_close($ch);

            $this->_data = $this->fetch_xml($data);

		}
		if (!$this->_data&&$mbuh=='add') {
            echo "ndak ada id";
			$this->_data = new stdClass();
			$this->_data->id = 0;
			$this->_data->name = null;
		}
		return $this->_data;
	}


	function store($post)
	{
	$serverName = $_SERVER['SERVER_NAME'];
    $data = JRequest::get( 'post' );
       $url_server = $this->getUrl();
      $datas = $data;
      $tujuan = $datas['tujuan'];
      //$url = "http://localhost/final/index.php/$tujuan/";
      $url = $url_server.$tujuan;
      $url_array = array ('url'=>$url);
	  $cekUrl = array ('domain'=>$serverName);
	  
      $datas =array_merge($datas, $cekUrl, $url_array);
	echo "<br />";
		print_r($datas);
echo "<br />";
      //$iin= array('mhs'=>"taufix");
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,$url );
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch,CURLOPT_POSTFIELDS,$datas);
      $test = curl_exec($ch);

      curl_close($ch);
	
	return $test;
	}

    function getUrl (){
       $config_params =& JComponentHelper::getParams( 'com_restoran' );
       $url_server = $config_params->get('url_server');

       return $url_server;
    }

	function delete($cid)
	{
		$serverName = $_SERVER['SERVER_NAME'];
		$cekUrl = array ('domain'=>$serverName);
        $cid = JRequest::getVar( 'cid', array(), '', 'array' );
        $config_params =& JComponentHelper::getParams( 'com_restoran' );
        $apikey = $config_params->get('api_key');
        foreach ($cid as $key=>$value){
            $recId[]=$value;
        }
        $stringRec = join (',',$recId);
        $recsnoArray = array ('recsno'=>$stringRec);


        $url_server = $this->getUrl();
        $tujuan = "food";

        // mendefiniskan url yang dituju
        $url = $url_server.$tujuan;
        // menaruh url dalam array
        $url_array = array ('url'=>$url);
        $api = array('api'=>$apikey);

        $flag = array('flag'=>'delete');

        $datas =array_merge($recsnoArray,$api,$cekUrl,$flag, $url_array);

       // print_r($datas);

          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url );
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch,CURLOPT_POSTFIELDS,$datas);
          curl_exec($ch);

          curl_close($ch);


		return true;
	}// end of function delete

    function fetch_xml($xml){

    if(is_file($xml)){
        $xml_data = file_get_contents($xml);
    }
    else{
        $xml_data = $xml;
    }
    $parser = xml_parser_create();
    xml_parse_into_struct($parser, $xml_data, &$assoc_arr, &$idx_arr);
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    $root_tag = $assoc_arr[0]['tag'];
    $base_tag = strtolower($assoc_arr[1]['tag']);
    $i = 0;
    foreach($assoc_arr as $key => $element){
        if($element['tag'] != $root_tag){
            if(!preg_match('/^\s+$/', $element['value'])){
                $tag = strtolower($element['tag']);
                $items[$i][$tag] = $element['value'];
                if($tag == $base_tag){
                    $i++;
                }
            }
            elseif(isset($element['attributes'])){
                $items[$i]['id'] = $element['attributes']['ID'];
            }
        }
    }

    return $items;
    } //end of function fetchxml

}
